<?php

namespace app\Middlewares;

use app\Models\AdminModel as Admin;

class AdminMiddleware {
	/**
	 * Example middleware invokable class
	 *
	 * @param  \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
	 * @param  \Psr\Http\Message\ResponseInterface      $response PSR7 response
	 * @param  callable                                 $next     Next middleware
	 *
	 * @return \Psr\Http\Message\ResponseInterface
	 */
	public function __invoke($request, $response, $next) {
		$username = $request -> getParam("username");
		$model = new Admin();
		$cookieReturn["token"] = bin2hex(openssl_random_pseudo_bytes(16));
		$cookieReturn["expiretime"] = date("Y-m-d H:i:s", time() + 3600);
		$model -> updateTokenModel($username, $cookieReturn["token"], $cookieReturn["expiretime"]);
		setcookie("token", $cookieReturn["token"]);
		$response = $next($request, $response);
		return $response;
	}

}
?>